﻿Imports System.Windows.Visibility
Imports System.Windows.Threading.DispatcherPriority

Partial Public Class AlbumSelectorControl

    Private Sub AlbumSelectorControl_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        Try
            AddHandler g_album_mgr.AlbumsChanged, AddressOf OnAlbumsChanged
            AddHandler g_album_mgr.CurrentAlbumChanged, AddressOf OnCurrentAlbumChanged
            AddHandler g_album_mgr.AlbumLoaded, AddressOf OnAlbumLoaded
            AddHandler g_album_mgr.StartAlbumLoad, AddressOf OnStartAlbumLoad
            AddHandler g_connection.ConnectionChanged, AddressOf OnConnectionChanged
            LoadAlbums()
        Catch ex As Exception
        End Try
    End Sub

    Private m_mapcontrol As VirtualEarthMap.IMapControl

    Public WriteOnly Property MapControl() As VirtualEarthMap.IMapControl
        Set(ByVal value As VirtualEarthMap.IMapControl)
            If m_mapcontrol IsNot Nothing Then
                RemoveHandler m_mapcontrol.LoadingPhotos, AddressOf OnMapControlLoadingPhotos
                RemoveHandler m_mapcontrol.LoadingWebpage, AddressOf OnMapControlLoadingWebpage
                RemoveHandler m_mapcontrol.LoadComplete, AddressOf OnMapControlLoadComplete
            End If
            m_mapcontrol = value
            AddHandler m_mapcontrol.LoadingPhotos, AddressOf OnMapControlLoadingPhotos
            AddHandler m_mapcontrol.LoadingWebpage, AddressOf OnMapControlLoadingWebpage
            AddHandler m_mapcontrol.LoadComplete, AddressOf OnMapControlLoadComplete
        End Set
    End Property

    Private Sub OnMapControlLoadingPhotos()
        lblProgress.Content = "loading photos into Virtual Earth map"
        'Trace.WriteLine(lblProgress.Content)
        'SyncControls(True)
    End Sub

    Private Sub OnMapControlLoadingWebpage()
        lblProgress.Content = "loading Virtual Earth web page"
        'Trace.WriteLine(lblProgress.Content)
        'SyncControls(True)
    End Sub

    Private Sub OnMapControlLoadComplete()
        'lblProgress.Content = "Virtual Earth map loaded"
        'Trace.WriteLine(lblProgress.Content)
        'SyncControls(False)
    End Sub

    Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
        LoadAlbum()
    End Sub

    Private Sub cbxAlbums_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As SelectionChangedEventArgs) Handles cbxAlbums.SelectionChanged
        LoadAlbum()
    End Sub

    Private Sub LoadAlbum()
        Try
            Dim a As Album = DirectCast(cbxAlbums.SelectedItem, Album)
            cbxAlbums.Text = a.AlbumName
            g_album_mgr.CurrentAlbum = a
            g_album_mgr.LoadAlbum()
        Catch
        End Try
    End Sub

    Private Delegate Sub DlgLoadAlbums()
    Private Sub LoadAlbums()
        If Not Dispatcher.CheckAccess() Then
            Dispatcher.Invoke(Normal, New DlgLoadAlbums(AddressOf LoadAlbums))
        Else
            cbxAlbums.BeginInit()
            cbxAlbums.Items.Clear()
            For Each a As Album In g_album_mgr.Albums.Values
                cbxAlbums.Items.Add(a)
                If a Is g_album_mgr.CurrentAlbum Then cbxAlbums.SelectedItem = a
            Next
            If cbxAlbums.Items.Count = 0 Then cbxAlbums.Text = ""
            cbxAlbums.EndInit()
            If g_album_mgr.CurrentAlbum Is Nothing Then
                cbxAlbums.ToolTip = ""
            Else
                cbxAlbums.ToolTip = g_album_mgr.CurrentAlbum.Query
            End If
        End If
    End Sub

    Private Sub OnConnectionChanged()
        If g_connection.Connected Then
            LoadAlbums()
        Else
            LoadAlbums()
        End If
    End Sub

    Private Sub OnAlbumsChanged()
        LoadAlbums()
    End Sub

    Private Sub OnCurrentAlbumChanged(ByVal currentalbum As Album)
        SyncControls(True)
    End Sub

    Private Sub OnAlbumLoaded()
        SyncControls(False)
    End Sub

    Private Sub OnStartAlbumLoad()
        SyncControls(True)
    End Sub


    Private Delegate Sub DlgSyncControls(ByVal busy As Boolean)
    Private Sub SyncControls(ByVal busy As Boolean)
        If Not Dispatcher.CheckAccess() Then
            Dispatcher.Invoke(Normal, New DlgSyncControls(AddressOf SyncControls), busy)
        Else
            cbxAlbums.Visibility = IIf(busy, Hidden, Visible)
            btnLoad.Visibility = IIf(busy, Hidden, Visible)
            lblProgress.Visibility = IIf(busy, Visible, Hidden)
            'Trace.WriteLine("SyncControls busy: " & busy)

            If Not busy Then
                Try
                    cbxAlbums.ToolTip = g_album_mgr.Photos.Rows.Count & " photo(s) retrieved by query: " & g_album_mgr.CurrentAlbum.Query
                Catch
                    cbxAlbums.ToolTip = "<no query>"
                End Try
                Try
                    'CONVERT:lblAlbumStats.Text = g_album_mgr.Photos.Rows.Count & " photo(s) loaded."
                Catch ex As Exception
                    'CONVERT:lblAlbumStats.Text = "Album not loaded."
                End Try
            End If
        End If
    End Sub

End Class
